package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Login;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Tel;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.UpdateVersion;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.DataManager;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.receiver.NetworkConnectChangedReceiver;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.IsVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainUdis;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 作者：Administrator on 2017/6/26 16:46
 * 邮箱：zhanghuaiha@gmail.com
 */

public class SplashActivity extends BaseActivity {

    private DataManager dataManager;
    private CompositeSubscription mCompositeSubscription;
    private static final int UPDATA_VERSION = 1;
    private static final int ODL_VERSION = 2;
    private static final int TEL_USABLE = 3;
    private static final int TEL_NOT_USABLE = 4;
    private static final int NO_NET = 5;
    private static final int NO_NET_TWO = 6;
    private TextView no_net;

    Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //接收Message发送的消息  msg.what  msg.arg1  msg.arg2  msg.obj
            switch (msg.what) {
                case TEL_USABLE:
                    jumpToActivity(MainActivity.class);
                    break;
                case TEL_NOT_USABLE:
                    jumpToActivity(LoginActivity.class);
                    break;
                case NO_NET:
                    break;
            }
            finish();
        }
    };
    private Dialog dialog;
    private String versionCode;
    private String udid;

    @Override
    public void initView() {
        versionCode = ObtainVersion.getVersionName(this);
        udid = ObtainUdis.getUDID(SplashActivity.class);
        no_net = (TextView) findViewById(R.id.no_net);
        dataManager = new DataManager(this);
        mCompositeSubscription = new CompositeSubscription();
        initDateS();
    }

//    //获取最新版本
//    private void initVersion() {
//        String time = System.currentTimeMillis() + "";
//        String encry = ObtainEncry.getVersion("2", time, versionCode, NetWorkCodeInfo.VERSION, udid);
//        mCompositeSubscription.add(dataManager.getVersion("2",time,versionCode,NetWorkCodeInfo.VERSION,udid,encry)
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//        .subscribe(new Observer<UpdateVersion>() {
//            @Override
//            public void onCompleted() {
//            }
//            @Override
//            public void onError(Throwable e) {
//                no_net.setVisibility(View.VISIBLE);
//            }
//            @Override
//            public void onNext(UpdateVersion updateVersion) {
//                Log.e("SplashActivity",updateVersion.getCode()+"");
//                if (updateVersion.getCode() == 200) {
//                    int i = IsVersion.compareVersion(versionCode, updateVersion.getData().getAndroid_number());
//                    if (i == 1) {
//                        showForceUpdateDialog(updateVersion);
//                    }else {
//
//                    }
//                }
//            }
//        }));
//    }


    //判断该用户手机号是否作废
    private void initDateS() {
        String tel = App.SP.getString("tel", "");
        String id = App.SP.getString("id", "");
        if (tel.equals("") || id.equals("")) {
            handler.sendMessageDelayed(handler.obtainMessage(TEL_NOT_USABLE), 2000);
        }else {
            String time = System.currentTimeMillis() + "";
            String encry = ObtainEncry.getCode("2", time, versionCode, NetWorkCodeInfo.TEL, udid, tel);
            mCompositeSubscription.add(dataManager.getTel("2",time,versionCode, NetWorkCodeInfo.TEL,udid,tel,encry)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Tel>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            no_net.setVisibility(View.VISIBLE);
                            handler.sendMessageDelayed(handler.obtainMessage(NO_NET), 3000);
                        }

                        @Override
                        public void onNext(Tel tel) {
                            if (tel.getCode().equals("200")) {
                                handler.sendMessageDelayed(handler.obtainMessage(TEL_USABLE), 2000);
                            }else {
                                handler.sendMessageDelayed(handler.obtainMessage(TEL_NOT_USABLE), 2000);
                            }
                        }
                    }));
        }
    }


    @Override
    public void initData() {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription.hasSubscriptions()){
            mCompositeSubscription.unsubscribe();
        }
    }


//    private void showForceUpdateDialog(final UpdateVersion info){
//        if (dialog == null){
//            dialog = new Dialog(this,R.style.Theme_Light_Dialog);
//        }
//        View dialogview = View.inflate(this, R.layout.dialog_update, null);
//        TextView dialog_tittle = (TextView) dialogview.findViewById(R.id.updatedialog_tittle);
//        TextView dialog_info = (TextView) dialogview.findViewById(R.id.updatedialog_info);
//        TextView update = (TextView) dialogview.findViewById(R.id.update);
//        dialog.setContentView(dialogview);
//        dialog.setCancelable(false);
//        String string = info.getData().getContent() != null ? (String)info.getData().getContent() : "";
//        dialog_info.setText("更新内容： "+string);
//        dialog_tittle.setText("发现新版本： "+info.getData().getAndroid_number());
//        update.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.setAction("android.intent.action.VIEW");
//                Uri content_url = Uri.parse(info.getData().getAndroid_url());
//                intent.setData(content_url);
//                startActivity(intent);
//            }
//        });
//        dialog.show();
//    }
}
